package woa.linkedlist;

/**
 * 相交链表
 * 给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点，返回 null
 *
 * @author wangpeng
 * @date 2021/11/20
 */
public class GetIntersectionNodeProblem {

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }

    public static void main(String[] args) {
        ListNode headA = new ListNode(1, new ListNode(2));
        ListNode headB = new ListNode(4, new ListNode(5, new ListNode(6)));
        getIntersectionNode(headA, headB);
    }
}
